
package com.thoughtworks;

import java.util.Scanner;

class LibraryApplication {
    public static void main(String[] args) throws NoSuchEntity, NotAValidUser {


        Library library = new Library();

        String bookName1;
        String userName = "";
        boolean validUser=false;
        String libraryNumber = "", password = "";
        Scanner input = new Scanner(System.in);
        int choice = 2, initialChoice = 1;
        do {
            System.out.println("choose what you wish to do: .... 1.Browse book   2.Login   3.Quit");
            initialChoice = input.nextInt();
            if (initialChoice == 1)
                library.listAllEntities();
            else if (initialChoice == 2) {

                try {


                    System.out.println("Please enter your library number");
                    libraryNumber = input.next();
                    System.out.println("Enter your  password");
                    password = input.next();


                    validUser = library.logIn(libraryNumber, password);
                    userName=library.getUserName(libraryNumber);
                    if(validUser)
                    System.out.println("Welcome " + userName);


                } catch (Exception e) {
                    System.out.println(e);
                }
            } else if (initialChoice == 3) {
                System.out.println("thank you for visiting the library!");
                System.exit(0);
            }
            if (validUser) {
                do {

                    System.out.println();
                    if(userName.equals("Librarian"))
                    System.out.println("Choose what you wish to do..... 1.Browse books/movies   6.View my profile   7.Quit  8.Log out 9.Who took this book?");
                    else
                        System.out.println("Choose what you wish to do..... 1.Browse books/movies   2.Check out Book  3.Return Book  4.Checkout movie  5.Return movie 6.View my profile   7.Quit  8.Log out");
                    choice = input.nextInt();
                    input.nextLine();

                    switch (choice) {
                        case 1:
                            library.listAllEntities();
                            break;


                        case 2: {

                            System.out.println("enter the book Name to check out");
                            bookName1 = input.nextLine();
                            try {
                                library.checkOut(bookName1, libraryNumber, Book.class);
                                //librarian.checkedOutBooks.put(libraryNumber,bookName1);
                            } catch (Exception e) {
                                System.out.println(e);
                            }
                        }
                        break;
                        case 3: {

                            System.out.println("enter the book name to return");
                            bookName1 = input.nextLine();
                            try {
                                library.returnRentable(bookName1, libraryNumber, Book.class);
                                //librarian.checkedOutBooks.remove(libraryNumber,libraryNumb);

                            } catch (Exception e) {
                                System.out.println(e);
                            }
                        }
                        break;
                        case 4: {
                            System.out.println("enter the movie to be checked out");
                            String movieName = input.nextLine();
                            try {
                                library.checkOut(movieName, libraryNumber, Movie.class);

                            } catch (Exception e) {
                                System.out.println(e);
                            }

                        }
                        break;
                        case 5: {
                            System.out.println("Enter the movie name to be returned");
                            String movieName = input.nextLine();
                            try {
                                library.returnRentable(movieName, libraryNumber, Movie.class);


                            } catch (Exception e) {
                                System.out.println(e);
                            }

                        }
                        break;
                        case 6:
                        {
                            library.seeMyDetails(libraryNumber);
                        }
                        break;
                        case 7: {
                            System.out.println("Thank you for visiting our library ! ");
                            System.exit(0);

                        }
                        break;
                        case 8:
                        { libraryNumber = library.logOut(libraryNumber);

                        }
                            break;
                        case  9: {
                            System.out.println("Enter the entity name");
                            String entityName= input.nextLine();
                            String rentedUser="";
                            rentedUser=library.whoHasThisEntity(entityName);
                            System.out.println("user"+rentedUser+"has "+entityName);
                        }
                        break;
                        default:
                            System.out.println("Select a valid option");

                    }


                } while (choice != 8);


            } else
                System.out.println("please login to continue...");
        } while (initialChoice != 3);
    }

}
